package com.gzy.xuexiziliao.service.impl;


import com.gzy.xuexiziliao.mapper.DoctorMapper;
import com.gzy.xuexiziliao.mapper.StudentMapper;
import com.gzy.xuexiziliao.pojo.entity.AllReport;
import com.gzy.xuexiziliao.service.RePortService;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RePortServiceImpl implements RePortService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public AllReport StudenStatistics(LocalDate begin, LocalDate end) {
        //获取需要的日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> newStudentList = new ArrayList<>();
        List<Integer> totalStudentList = new ArrayList<>();
        for (LocalDate date : dateList){
            //设置当天的最小时间与最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //查询date当天的总用户数
            Map map = new HashMap();
            map.put("end",endTime);
            Integer totalStudent = studentMapper.countByMap(map);
            //查询date当天的新增用户数
            map.put("begin",beginTime);
            Integer newStudent = studentMapper.countByMap(map);
            totalStudentList.add(totalStudent);
            newStudentList.add(newStudent);
        }
        return AllReport.builder()
                .dateList(StringUtils.join(dateList.toString(), ","))
                .totalList(StringUtils.join(totalStudentList.toString(), ","))
                .newList(StringUtils.join(newStudentList.toString(), ","))
                .build();
    }

    @Override
    public AllReport DoctorStatistics(LocalDate begin, LocalDate end) {
        //获取需要的日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> newDoctorList = new ArrayList<>();
        List<Integer> totalDoctorList = new ArrayList<>();
        for (LocalDate date : dateList){
            //设置当天的最小时间与最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //查询date当天的总用户数
            Map map = new HashMap();
            map.put("end",endTime);
            Integer totalDoctor = doctorMapper.countByMap(map);
            //查询date当天的新增用户数
            map.put("begin",beginTime);
            Integer newDoctor = doctorMapper.countByMap(map);
            totalDoctorList.add(totalDoctor);
            newDoctorList.add(newDoctor);
        }
        return AllReport.builder()
                .dateList(StringUtils.join(dateList.toString(), ","))
                .totalList(StringUtils.join(totalDoctorList.toString(), ","))
                .newList(StringUtils.join(newDoctorList.toString(), ","))
                .build();
    }

}

